<?php
namespace My\Controller;

use Utuo\BackgroundController;

class SiteCategoryController extends BackgroundController {
	
	private $tplTypes = array('page','cate','list','show');
	
	private $moduleId = null;
	
	public function _initialize() {
		$this->setControllerTitle("栏目管理");
		$this->setActionTitle(array(
			'index' => '栏目列表',
			'add' => '栏目添加',
			'modify' => '修改栏目',
			'recycle' => '回收站'
		));
		$this->setActionNav(array(
			'index',
			'add',
			'recycle'
		));
		$this->moduleId = M("Model")->where("tag='%s'", 'Site')->getField("id"); 
	}
	
	// 栏目列表
	public function indexAction() {
		$d = D("ArticleCategory");
		$tree = $d->getTree(0, $this->aid, 'id,pid,name,type,sort,status,visible,is_deleted,logo,level,has_child');
		$categoryLists = $d->getTreeList($tree);
		foreach ($categoryLists as &$v){
			$v['type'] = $d->types[$v['type']];
		}
		$this->assign('articleCategoryLists', $categoryLists);
		$this->display();
	}
	
	// 添加栏目
	public function addAction() {
		C("TOKEN_ON", true);
		$d = D("ArticleCategory");
		$pid = I("get.id",0,"intval");
		$categoryOptions = $d->getOptionTree(null, $this->aid, 'id,pid,name');
		/*页面输出*/
		$contentModels = M("ContentModel")->where("module_id=%d", $this->moduleId)->getField("model,name");
		$visibleOptions = $this->getRadio('visible', array('0'=>'显示', '1'=>'隐藏'), 0);
		$isopenOptions = $this->getRadio('isopen', array('0'=>'显示', '1'=>'隐藏'), 0);
		$this->assign('visibleOptions',$visibleOptions);
		$this->assign('isopenOptions',$isopenOptions);
		$this->assign("contentModelsOptions", $this->getOption($contentModels, null, '==请选择列表内容类型=='));
		$this->assign("articleCategoryOptions", $this->getOption($categoryOptions, "$pid", '==顶级栏目=='));
		$this->assign("typesOptions", $this->getOption($d->types,'','==请选择栏目类型=='));
		$this->display();
	}
	
	// 提交添加栏目
	public function addPostAction() {
		C("TOKEN_ON", true);
		$d = D("ArticleCategory");
		if ($d->create()) {
			if($d->pid){
				$m = M("ArticleCategory");
				$pLevel = $m->where("id=%d", $d->pid)->getField("level");
				$d->level = $pLevel + 1;
				$m->where("id=%d",$d->pid)->setField('has_child',1);
			}
			$d->cid = $this->aid;
			if ($d->add()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 修改栏目
	public function modifyAction() {
		$id = I("id", 0, "intval");
		$d = D("ArticleCategory");
		$this->checkAdmin(null, $d, "cid");
		$category = $this->getOneById("ArticleCategory");
		$categoryOptions = $d->getOptionTree($id, $this->aid, 'id,pid,name');
		/*获得模板配置*/
		$tplConfig = unserialize($category['tpl_config']);
		$site = M("Site")->where(array('cid'=>$this->aid))->find();
		foreach ($this->tplTypes as $v) {
			$tplsList = array();
			$site['theme'] = $site['theme']*1;
			if( $site['theme'] ) { 
				/* 获得当前主题的各个模板类型可设置的模板 */
				$tpls = M("ThemeTplAuth a")->join("__THEME_TPL__ b on a.tpl_id=b.id")->where("b.is_deleted=0 and b.status=1 and b.type='%s' and find_in_set({$site['theme']},a.theme)", $v)->field('b.id,b.name')->select();
				foreach ( $tpls as $v0) $tplsList[$v0['id']] = $v0['name'];
			}
			$category[$v."TplOptions"] = $this->getOption($tplsList, $tplConfig[$v], '==默认模板==');
		}
		/*页面输出*/
		$contentModels = M("ContentModel")->getField("model,name");
		$category['visibleOptions'] = $this->getRadio('visible', array('0'=>'显示', '1'=>'隐藏'), $category['visible']*1);
		$category['isopenOptions'] = $this->getRadio('isopen', array('0'=>'显示', '1'=>'隐藏'), $category['isopen']*1);
		$category['categoryOptions'] = $this->getOption($categoryOptions, $category['pid'], '==顶级栏目==');
		$category["typesOptions"] = $this->getOption($d->types,$category['type'],'==请选择栏目类型==');
		$category['contentModelsOptions'] = $this->getOption($contentModels, $category['content_model'], '==请选择列表内容类型==');
        $category['imageArray']=explode(",", $category['image']);
        array_pop($category['imageArray']);
      
	    $this->assign("articleCategory", $category);
		$this->display();
	}
	
	// 提交修改栏目
	public function modifyPostAction() {
		$id = I("id", 0, "intval");
		$d = D("ArticleCategory");
		if ($d->create()) {
			$d->cid = $this->aid;
			/*父栏是否有变动*/
			$category = $this->getOneById("ArticleCategory"); $d->pid*=1;
			if($category['pid'] != $d->pid) { // 父栏有变动 
				$d->changeParentNode($category, 'id,pid,name');
				$d->getError() && $this->ajaxError($d->getError()); 
			}
			if (false !== $d->save()) {
				$this->ajaxSuccess();
			}
		}
		$this->ajaxError($d->getError());
	}
	
	// 回收站
	public function recycleAction() {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$condition = array(
				'is_deleted' => 1,
				"cid" => $this->aid
		);
		$articleCategoryLists = M("ArticleCategory")->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$articleCategoryCount = M("ArticleCategory")->where($condition)->count();
		$this->assign('articleCategoryLists', $articleCategoryLists);
		$this->showPage($articleCategoryCount, $pagesize);
		$this->display();	
	}
	
	// 恢复栏目
	public function restorePostAction() {
		$this->checkAdmin(null, M("ArticleCategory"), "cid");
		$m = M("ArticleCategory");
		$category = $this->getOneById("ArticleCategory");
		// 有父栏先恢复父栏
		if($category['pid']) {
			$parentCategory = $m->find($category['pid']);
			if($parentCategory['is_deleted']) {
				$this->ajaxError("存在父栏目必须先恢复父栏目.");
			}
			// 设置父栏有孩子
			$m->where("id=%d",$parentCategory['id'])->setField('has_child', 1);
		}
		$this->ajaxRestore("ArticleCategory");
	}
	
	// 移除栏目
	public function removePostAction() {
		$id = I('id', 0, 'intval');
		$m = M("ArticleCategory");
		$this->checkAdmin(null, M("ArticleCategory"), "cid");
		// 有子栏则不能删除
		if($m->where("pid=%d and is_deleted=0",$id)->count()) {
			$this->ajaxError("存在子栏目未能删除");
		}
		$this->addEventListener("afterRemove", function($e, $scope) {
			// 设置父栏无孩子
			$id = I('id', 0, 'intval');
			$m = M("ArticleCategory");
			$category = $m->where("id=%d", $id)->find();
			if( $category['pid'] ) {
				$count = $m->where("pid=%d and is_deleted=0", $category['pid'])->count();
				$count or $m->where("id=%d", $category['pid'])->setField('has_child',0); 
			}
		});
		$this->ajaxRemove("ArticleCategory");
	}
	
	
	// 删除栏目
	public function deletePostAction() {
		$id = I('id', 0, 'intval');
		$m = M("ArticleCategory");
		$this->checkAdmin(null, $m, "cid");
		// 有子栏则不能删除
		if($m->where("pid=%d",$id)->count()){
			$this->ajaxError("存在子栏目未能删除");
		}
		$this->ajaxDelete("ArticleCategory");
	}
	
	
	
	// 设置状态
	public function setStatusPostAction() {
		$this->checkAdmin(null, M("ArticleCategory"), "cid");
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$category = M("ArticleCategory");
		if ($category->where("id={$id}")->setField("status", 1 - $status)) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($category->getError());
		}
	}
	
	
}

